<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="phone"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import md5 from 'md5'
import {setToken} from '../../util/auth'

import { Toast } from "vant";
import { login } from "../../api/login";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  props: [],
  components: {},
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit(values) {
      console.log("submit", values);
      let {phone,password} = values
      let res = await login({phone,md5_password:md5(password)});
      console.log(res);
      if (res.code == 200) {
        Toast.success("登录成功");
        setToken('cookie',res.cookie)
        localStorage.setItem("uid", res.account.id);
        localStorage.setItem("userObj", JSON.stringify(res.profile));
        this.$store.commit("setuid", res.account.id);
        // console.log(localStorage.getItem('uid'),this.$store.state.uid);
          this.$router.push("/home");
      } else {
        Toast.fail(res.message);
      }
    },
  },
};
</script>

<style scoped>
</style>
